package com.zymail.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyChannel;
import com.zymail.bean.ZyGood;
import com.zymail.bean.ZyGoodDe;
import com.zymail.bean.ZyGoodImg;
import com.zymail.service.ZyChannelService;
import com.zymail.service.ZyGoodDeService;
import com.zymail.service.ZyGoodImgService;
import com.zymail.service.ZyGoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/util")
@Slf4j
public class UtilController extends BaseController{

    @Autowired
    ZyChannelService channelService;

    @Autowired
    ZyGoodDeService goodDeService;

    @Autowired
    ZyGoodImgService imgService;

    /**
     * 得到首页的channel
     * @return  List<Zychannel>
     */
    @GetMapping("/mainChannel")
    public List<ZyChannel> get_main_channel(){
        QueryWrapper<ZyChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("role", 1);  //1代表首页channel
        return channelService.list(wrapper);
    }

    /**
     * 获取
     * @return  List<Zychannel>
     */
    @GetMapping("/all")
    public Map<String,Object> get_all_channel(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<ZyChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("role", 1);  //1代表首页channel
        List<ZyChannel> zychannels = channelService.list(wrapper);
        map.put("code",0);
        map.put("data",zychannels);
        return map;
    }

    /**
     * 通过类别id得到图片
     * @param deId  类别id
     * @return  图片类
     */
    @GetMapping("/getimgbydeid/{deId}")
    public ZyGoodImg getimgBydeId(@PathVariable("deId") Integer deId){
        ZyGoodDe de = goodDeService.getById(deId);

        QueryWrapper<ZyGoodImg> wrapper = new QueryWrapper<>();
        wrapper.eq("good_id", de.getGoodId());
        wrapper.eq("type", 1);
        return imgService.getOne(wrapper);
    }



    @PostMapping("/channel/add")
    public String channeladd(ZyChannel zyChannel){
        log.info("add channel " + zyChannel);
        boolean i = channelService.save(zyChannel);
        if(i){
            return "succ";
        }
        return "fail";
    }
    @PostMapping("/channel/edit")
    public String channeledit(ZyChannel zyChannel){
        boolean i = channelService.saveOrUpdate(zyChannel);
        if(i){
            return "succ";
        }
        return "fail";
    }
    @PostMapping("/channel/del")
    public String channeldel(ZyChannel zyChannel){
        boolean i = channelService.removeById(zyChannel.getId());
        if(i){
            return "succ";
        }
        return "fail";
    }
    /**
     * 得到首页的channel
     * @return  List<Zychannel>
     */
    @GetMapping("/channel/id")
    public ZyChannel channel_id(Integer id){
        QueryWrapper<ZyChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return channelService.getOne(wrapper);
    }

}
